home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_06_07 / v6n7066a.txt < prev    next >
Text File  |  1989-09-26  |  2KB  |  87 lines

  1. /****************************  Listing 3  ****************************/
  2. /*******                                                       *******/
  3. /*******   Add this code at the end of the program section     *******/
  4. /*******       of yref2.y, after first deleting main()         *******/
  5. /*******                                                       *******/
  6. /*********************************************************************/
  7.  
  8. main()
  9. {
  10.          char word[32];
  11.          root = NULL;
  12.  
  13.          yyparse();
  14.          treeprint(root);
  15.  
  16. }
  17.  
  18. struct tnode *addtree(struct tnode *p, char *w)
  19. {
  20.         int cond;
  21.         struct symlist *list;
  22.  
  23.         if (p == NULL)
  24.                 {
  25.                 p = talloc();
  26.                 p->word = strdup(w);
  27.                 p->count = 1;
  28.                 p->first = p->current = lalloc();
  29.                 p->current->line = line_num;
  30.                 p->current->decl = yn_decl;
  31.                 p->left = p->right = NULL;
  32.                 }
  33.         else if ((cond = strcmp(w, p->word)) == 0)
  34.                 {
  35.                 p->count++;
  36.                 list = p->current;
  37.                 p->current = list->next = lalloc();
  38.                 p->current->line = line_num;
  39.                 p->current->decl = yn_decl;
  40.                 }
  41.         else if (cond < 0)
  42.                 p->left = addtree(p->left, w);
  43.         else
  44.                 p->right = addtree(p->right, w);
  45.         return p;
  46. }
  47.  
  48. void treeprint(struct tnode *p)
  49. {
  50.         int i;
  51.  
  52.         if (p != NULL)
  53.            {
  54.            treeprint(p->left);
  55.            printf("\n\n%-31s   Count:%4d\n    ", p->word, p->count);
  56.            listprint(p->first, p->count);
  57.            treeprint(p->right);
  58.            }
  59. }
  60.  
  61. void listprint(struct symlist *list, int c)
  62. {
  63.         struct symlist *n;
  64.         int i, j;
  65.  
  66.         for ( i = 0, j = 1, n = list; i < c; i++, j++, n = n->next)
  67.            {
  68.            printf(" %4d%c", n->line, n->decl ? '*' : ' ');
  69.            if ( j == 8 )
  70.               {
  71.               j = 0;
  72.               printf("\n    ");
  73.               }
  74.            }
  75. }
  76.  
  77. struct tnode *talloc(void)
  78. {
  79.         return (struct tnode *) malloc(sizeof(struct tnode));
  80. }
  81.  
  82. struct symlist *lalloc(void)
  83. {
  84.         return (struct symlist *) malloc(sizeof(struct symlist));
  85. }
  86.  
  87.